//
//  OptionScrollBtnVC.m
//  Longer3D
//
//  Created by 王大江 on 15/10/27.
//  Copyright © 2015年 longer3d. All rights reserved.
//

#import "NewOptionScrollBtnVC.h"

#define padding 10
#define hugeOrderFont [UIFont systemFontOfSize:16]
#define smallOrderFont [UIFont systemFontOfSize:14]

@interface NewOptionScrollBtnVC ()<UIScrollViewDelegate>
@property (nonatomic,weak) UIView *topView;
@property (nonatomic,strong) NSMutableArray *btnArray;
@property (nonatomic,weak) UIScrollView *scrollView;

@end

@implementation NewOptionScrollBtnVC
{
    //    NSArray *vcArray;
    NSMutableArray *lineArray;
    UIButton *selectBtn;
}


- (NSMutableArray *)btnArray
{
    if (!_btnArray) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    //    [self setupsubview];
}


- (void)setupsubviewWithViewcontrollerArray:(NSArray *)viewcontrollerArray AndTitleArray:(NSArray *)titleArray CGPoint:(CGPoint)point height:(CGFloat)fHeight {
    lineArray = [NSMutableArray array];
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(point.x, point.y, ScreenWidth, fHeight)];
    [self.view addSubview:topView];
    topView.backgroundColor = [UIColor whiteColor];
    topView.layer.borderColor = publicLineColor.CGColor;
    topView.layer.borderWidth = 1;
    [self topViewAddsubView:topView TitleArray:titleArray];
    self.topView = topView;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(topView.frame), ScreenWidth, ScreenHeight - CGRectGetMaxY(topView.frame))];
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    int i;
    for (i=0; i<viewcontrollerArray.count; i++) {
        UIViewController *vc = (UIViewController *)viewcontrollerArray[i];
        vc.view.frame = CGRectMake(i*ScreenWidth, 0, scrollView.frame.size.width, scrollView.frame.size.height);
        [scrollView addSubview:vc.view];
        [self addChildViewController:vc];
    }
    scrollView.contentSize = CGSizeMake(ScreenWidth*i, 0);
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
}



#pragma mark ---scrollview及子控制器

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int tag = (scrollView.contentOffset.x + ScreenWidth*0.5)/ScreenWidth;
    [self selectBtn:tag];
}


- (void)topViewAddsubView:(UIView *)topView TitleArray:(NSArray *)titleArray
{
    //    NSArray *titleArray = @[@"收藏记录",@"关注记录",@"订单记录"];
    long tab_count = titleArray.count;
    CGFloat btnW = ScreenWidth/tab_count;
    CGFloat btnH = topView.frame.size.height;
    for (int i = 0; i< titleArray.count; i++) {
        UIButton *btn = [[UIButton alloc] init];
        btn.tag = i;
        btn.size = CGSizeMake(ScreenWidth / tab_count, topView.height);
        btn.origin = CGPointMake(i * btn.width, 0);
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 0);
        NSString *imageName = [NSString stringWithFormat:@"icon_%d",i+1];
        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        
        UILabel *label = [[UILabel alloc] init];
        label.size = CGSizeMake(ScreenWidth / tab_count, topView.height*0.6);
        label.origin = CGPointMake(i * ScreenWidth / tab_count, topView.height*0.25);
        label.font = smallOrderFont;
        label.text = [NSString stringWithFormat:@"%@",titleArray[i]];
        label.textAlignment = NSTextAlignmentCenter;
        [topView addSubview:label];
        
        CGSize size = [label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:smallOrderFont,NSFontAttributeName, nil]];
        
        UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake((btnW - size.width)/2,btnH-5, size.width, 2)];
        [lineArray addObject:lineV];
        [btn addSubview:lineV];
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.btnArray addObject:btn];
        [topView addSubview:btn];
    }
    
    [self clickBtn:(UIButton *)self.btnArray[0]];
    
}

- (void)clickBtn:(UIButton *)btn
{
    int tag = (int)btn.tag;
    
    [self selectBtn:tag];
    
    self.scrollView.contentOffset = CGPointMake(ScreenWidth*tag, 0);
}

- (void)selectBtn:(int)tag
{
    int upTag = (int)selectBtn.tag;
    UIView *upline = lineArray[upTag];
    upline.backgroundColor = [UIColor clearColor];
    selectBtn.selected = NO;
    
    UIButton *btn = _btnArray[tag];
    btn.selected = YES;
    UIView *line = lineArray[tag];
    line.backgroundColor = [UIColor redColor];//Loger3dRandomColor;
    selectBtn = btn;
}


@end
